/*
 * DialogDelegate.cpp  0.4.0 / Dec 04, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "DialogDelegate.h"

// DialogDelegate :: Protected Constructor. ------------------------------------

/*!
 * \brief Constructs new DialogDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the Dialog java object
 */
DialogDelegate::DialogDelegate(JNIEnv *env, jobject obj)
  : WindowDelegate(env, obj, new QDialog())
{
  // empty constructor body
}

// DialogDelegate :: Native Calls. ---------------------------------------------

/*!
 * \brief Starts modal loop of the dialog.
 *
 * \param env the JNIEnv pointer
 * 
 * \return dialog result
 */
jint DialogDelegate::exec(JNIEnv *env)
{
  Q_ASSERT(env);
  SWING_REENTRANT_RETURN(exec, jint, SWING_ENV_ARG)
  return qDialog()->exec();
}

/*!
 * \brief Stops dialog modal loop.
 *
 * \param env the JNIEnv pointer
 * \param result the dialog result
 */
void DialogDelegate::done(JNIEnv *env, jint result)
{
  Q_ASSERT(env);
  SWING_REENTRANT(done, SWING_ENV_ARG, JINT_ARG(result))
  qDialog()->done(result);
}

// DialogDelegate :: Static Native Calls. --------------------------------------

/*!
 * \brief Returns new instance of the delegate object
 *
 * \param env the JNIEnv pointer
 * \param obj the Swing object
 * 
 * \return new instance of the delegate object
 */
jlong DialogDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new DialogDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}
